package elitech.vietnam.myfashion.view;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.dialog.ConnectionErrorDialog;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.dialog.ShippingDialog;
import elitech.vietnam.myfashion.dialog.WarningDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.OrderHistory;
import elitech.vietnam.myfashion.extend.AsyncTask;
import elitech.vietnam.myfashion.view.OrderView.OrderViewEvent;

public class BuyingListView extends LinearLayout implements OnClickListener {
	
	private static final int UPDATE_TIME = 180000;

	private MainActivity mContext;
	private LinearLayout mTitleCart, mTitleSuccess, mTitleStatus, mCartBill, mSuccessBill, mStatusBill;

	private TextView mTextCart, mTextStatus, mTextSuccess;
	private Timer mTimer;
	
	int mSTCount = 0, mSCCount = 0;

	public BuyingListView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.buyinglist, this);
		}

		mTitleCart = (LinearLayout) findViewById(R.id.buyinglist_titleCart);
		mTextCart = (TextView) findViewById(R.id.buyinglist_titleCart_text);
		mTitleStatus = (LinearLayout) findViewById(R.id.buyinglist_titleStatus);
		mTextStatus = (TextView) findViewById(R.id.buyinglist_titleStatus_text);
		mTitleSuccess = (LinearLayout) findViewById(R.id.buyinglist_titleSuccess);
		mTextSuccess = (TextView) findViewById(R.id.buyinglist_titleSuccess_text);
		mCartBill = (LinearLayout) findViewById(R.id.buyinglist_layCart);
		mStatusBill = (LinearLayout) findViewById(R.id.buyinglist_layStatus);
		mSuccessBill = (LinearLayout) findViewById(R.id.buyinglist_laySuccessBill);

		mTitleCart.setOnClickListener(this);
		mTitleStatus.setOnClickListener(this);
		mTitleSuccess.setOnClickListener(this);
		GetOrder();
		Timer();
	}

	public void refreshCart() {
		mCartBill.removeAllViews();
		mTextCart.setText(mContext.getString(R.string.mycart) + " (0)");
		
		if (mContext.mOrder.ListDetail.size() == 0) {
			TextView txtText = new TextView(mContext);
			txtText.setText(R.string.emptycart);
			txtText.setPadding(8, 8, 8, 8);
			mCartBill.addView(txtText);
			return;
		}

		OrderView orderView = new OrderView(mContext, mContext.mOrder);
		orderView.mlayTitle.setVisibility(View.GONE);
		orderView.mlayControl.setVisibility(View.VISIBLE);
		orderView.setEvent(new OrderViewEvent() {
			@Override
			public void onRemove() {
				refreshCart();
			}

			@Override
			public void onPurchase() {
				if (mContext.mOrder.ListDetail.size() == 0) {
					new WarningDialog(mContext, mContext.getString(R.string.emptycart)).show();
					return;
				}
				ShippingDialog dialog = new ShippingDialog(mContext, false);
				dialog.setOnDismissListener(new OnDismissListener() {
					@Override
					public void onDismiss(DialogInterface dialog) {
						refreshCart();
						GetOrder();
					}
				});
				dialog.show();
			}

			@Override
			public void onClear() {
				new OkCancelWarnningDialog(mContext, mContext.getString(R.string.areyousure)) {
					public void onOk() {
						mContext.mOrder = new Order();
						refreshCart();
					};
				}.show();
			}

			@Override
			public void onCancel() {}
		});
		mCartBill.addView(orderView);
		mContext.mLastCartCount = mContext.mOrder.ListDetail.size();
		mTextCart.setText(mContext.getString(R.string.mycart) + " (" + mContext.mLastCartCount + ")");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buyinglist_titleCart:
			if (mCartBill.getVisibility() == View.VISIBLE)
				mCartBill.setVisibility(View.GONE);
			else
				mCartBill.setVisibility(View.VISIBLE);
			break;
		case R.id.buyinglist_titleStatus:
			if (mStatusBill.getVisibility() == View.VISIBLE)
				mStatusBill.setVisibility(View.GONE);
			else
				mStatusBill.setVisibility(View.VISIBLE);
			break;
		case R.id.buyinglist_titleSuccess:
			if (mSuccessBill.getVisibility() == View.VISIBLE)
				mSuccessBill.setVisibility(View.GONE);
			else
				mSuccessBill.setVisibility(View.VISIBLE);
			break;
		}
	}

	public void GetOrder() {
		new CallServiceTask() {
			private ArrayList<OrderHistory> rs = new ArrayList<OrderHistory>();

			public void work() {
				try {
					rs = service.getOrderHistory(mContext.mUser.Id);
				} catch (Exception e) {}
			};
			protected void onPostExecute(Void result) {
				if (mContext == null)
					return;
				if (mContext.mLastCartCount != mContext.mOrder.ListDetail.size() || mContext.mOrder.ListDetail.size() == 0)
					refreshCart();
				if (rs == null) {
					if (!mContext.isConnectionErrorShow) {
						mContext.isConnectionErrorShow = true;
						new ConnectionErrorDialog(mContext) {
							public void TryAgain() {
								GetOrder();
							};
						}.show();
					}
					return;
				}

				mStatusBill.removeAllViews();
				mSuccessBill.removeAllViews();
				mSCCount = 0; mSTCount = 0;
				mTextSuccess.setText(mContext.getString(R.string.success) + " (0)");
				mTextStatus.setText(mContext.getString(R.string.status) + " (0)");

				for (int i = 0; i < rs.size(); i++) {
					OrderHistory order = rs.get(i);
					order.Order.ListDetail.addAll(order.Details);
					switch (order.Order.Status) {
					case 7:
					case 8:
					case 9:
						if (order.Order.isDelete == 0) {
							final OrderView orderView = new OrderView(mContext, order.Order);
							orderView.mlayTitle.setVisibility(View.VISIBLE);
							orderView.setEvent(new OrderViewEvent() {
								@Override
								public void onRemove() {}

								@Override
								public void onPurchase() {}

								@Override
								public void onClear() {}

								@Override
								public void onCancel() {
									new OkCancelWarnningDialog(mContext, mContext.getString(R.string.areyousure)) {
										public void onOk() {
											new CallServiceTask() {
												int rs = -1;

												public void work() {
													try {
														rs = service.deleteOrder1(orderView.mOrder.Id);
													} catch (Exception e) {
														e.printStackTrace();
													}
												};

												@Override
												protected void onPostExecute(Void result) {
													if (rs != -1 && rs != 0) {
														mSuccessBill.removeView(orderView);
														mTextSuccess.setText(mContext.getString(R.string.success) + " (" + (mSCCount -= 1) + ")");
													}
												}

											}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
										};
									}.show();

								}
							});
							mSuccessBill.addView(orderView);
							mTextSuccess.setText(mContext.getString(R.string.success) + " (" + (mSCCount += 1) + ")");
						}
						break;
					default:
						final OrderView orderView = new OrderView(mContext, order.Order);
						orderView.mlayTitle.setVisibility(View.VISIBLE);
						orderView.setEvent(new OrderViewEvent() {
							@Override
							public void onRemove() {}

							@Override
							public void onPurchase() {}

							@Override
							public void onClear() {}

							@Override
							public void onCancel() {
								new OkCancelWarnningDialog(mContext, mContext.getString(R.string.areyousure)) {
									public void onOk() {
										new CallServiceTask() {
											int rs = -2;
											public void work() {
												try {
													rs = service.deleteOrder(orderView.mOrder.Id);
												} catch (Exception e) {
													e.printStackTrace();
												}
											};

											@Override
											protected void onPostExecute(Void result) {
												if (rs > 0) {
													mStatusBill.removeView(orderView);
													mTextStatus.setText(mContext.getString(R.string.status) + " (" + (mSTCount -= 1) + ")");
												} else if (rs == 0) {
													GetOrder();
												} else if (rs == -1) {
													
												}
											}
										}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
									};
								}.show();

							}
						});
						mStatusBill.addView(orderView);
						mTextStatus.setText(mContext.getString(R.string.status) + " (" + (mSTCount += 1) + ")");
						break;
					}
				}
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	public void Timer() {
		mTimer = new Timer();
		mTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (mContext.mUser != null) {
					Log.w("Shopping", "Auto Update");
					GetOrder();
				}
			}
		}, 0, UPDATE_TIME);
	}
}